import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutionException;

import javax.swing.JTextArea;
import javax.swing.SwingWorker;


class NewsReader extends SwingWorker<String, String> {
    
    private File file;
    private JTextArea text;

    public NewsReader (File file, JTextArea text){
        this.file = file;
        this.text = text;
    }
    
    @Override public String doInBackground(){
        
        StringBuilder result = new StringBuilder();
        BufferedReader reader = null;
        char[] buf = new char[1024];
        int r = 0;
        int readBytes = 0;
        long fileSize = file.length();
        int progressToDisplay = 0;
        
        try {
            reader = new BufferedReader(new FileReader(file));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
            
        try {
            while ((r = reader.read(buf)) != -1) {
                    result.append(buf, 0, r);
                    readBytes+=r;
                    progressToDisplay = (int) (readBytes/fileSize*100);
                    publish("Processed " + progressToDisplay);
                }
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally{
            try{
                reader.close();
            }
            catch(IOException e){
                e.printStackTrace();
            }
        }
         
        return result.toString();
    }
    
    @Override protected void done(){
        
        try {
            text.setText(get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        
    }
    
    @Override protected void process(List<String> progress){
        System.out.println(progress);
    }
    
}